package com.sandking.db.jedis;

import com.sandking.db.bean.Yhmrxx;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import com.sandking.db.dao.YhmrxxDao;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhmrxxJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhmrxx getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhmrxx yhmrxx = null;
		try{
			String key = "Yhmrxx_Object";
			String field = SK_Plus.b("id:",id).e();
			yhmrxx = Yhmrxx.createForJson(jedis.hget(key,field));
			if(yhmrxx == null){
				yhmrxx = YhmrxxDao.getById(id);
				if(yhmrxx!=null){
					loadCache(yhmrxx);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhmrxx;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static Yhmrxx getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhmrxx yhmrxx = null;
		try{
			String key = "Yhmrxx_Index";
			String field = SK_Plus.b("yh_id:",yh_id).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				yhmrxx = getById(Integer.valueOf(primaryKey));
			}
			if(yhmrxx == null){
				yhmrxx = YhmrxxDao.getByYh_id(yh_id);
				if(yhmrxx!=null){
					loadCache(yhmrxx);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhmrxx;
	}
	

	public static void loadCache(Yhmrxx yhmrxx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhmrxx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhmrxx yhmrxx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhmrxx_Object";
		String field = SK_Plus.b("id:",yhmrxx.getId()).e();
		String data = yhmrxx.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(yhmrxx.getId());
		key = "Yhmrxx_Index";
		
		field = SK_Plus.b("yh_id:",yhmrxx.getYh_id()).e();
		p.hset(key,field,primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhmrxx> yhmrxxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhmrxx yhmrxx : yhmrxxs){
				loadCache(yhmrxx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhmrxx yhmrxx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhmrxx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhmrxx yhmrxx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhmrxx_Object";
		String field = SK_Plus.b("id:",yhmrxx.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Yhmrxx_Index";
		
		field = SK_Plus.b("yh_id:",yhmrxx.getYh_id()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhmrxx> yhmrxxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhmrxx yhmrxx : yhmrxxs){
				clearCache(yhmrxx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhmrxx insert(Yhmrxx yhmrxx){
		yhmrxx = YhmrxxDao.insert(yhmrxx);
		if(yhmrxx!=null){
			loadCache(yhmrxx);
		}
    	return yhmrxx;
    }
    
    public static Yhmrxx update(Yhmrxx yhmrxx){
    	yhmrxx = YhmrxxDao.update(yhmrxx);
    	if(yhmrxx!=null){
			loadCache(yhmrxx);
		}
    	return yhmrxx;
    }
    
    public static boolean delete(Yhmrxx yhmrxx){
    	boolean bool = YhmrxxDao.delete(yhmrxx);
    	if(bool){
    		clearCache(yhmrxx);
    	}
    	return bool;
    }
}